<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<%= path %>/public/extjs/resources/css/ext-all.css" />
<link rel="stylesheet" type="text/css" href="<%= path %>/public/css/icon.css" />
</head>
<body topmargin="0">
<script type="text/javascript" src="<%= path %>/public/extjs/adapter/ext/ext-base.js"></script> 
<script type="text/javascript" src="<%= path %>/public/extjs/ext-all.js"></script> 
<div id="main" style="margin:10 0 0 10;"></div>
<div id="main2"></div>
<script type="text/javascript" >
	Ext.BLANK_IMAGE_URL  =  '<%= path %>/public/extjs/resources/images/default/s.gif';
	Ext.QuickTips.init();
	var pathUrl = '<%= path %>'; //该变量必须存在
	Ext.onReady(function(){
		//列模型
		var sm = new Ext.grid.CheckboxSelectionModel();
		var editorC=new Ext.grid.GridEditor(new Ext.form.TextField({allowBlank:false}));
		var editorNum=new Ext.grid.GridEditor(new Ext.form.NumberField({allowBlank:false}));
		//下拉框--begin
		var comStore=  new Ext.data.Store({
			proxy:new Ext.data.HttpProxy({
			    url:pathUrl+"/listJobTypes.do"
			}),
			reader:new Ext.data.JsonReader(
				{
					root:"list"},
				[{name:"jobTypeId",mapping:"jobTypeId"},
				 {name:"jobTypeName",mapping:"jobTypeName"}
				])
		});
		comStore.load();
		var editorBox=new Ext.form.ComboBox({
			id:'editorBox',
			store :comStore,
		    valueField :'jobTypeId',
		    displayField :'jobTypeName',
		    forceSelection :true,
		    typeAhead :true,
		    triggerAction :'all',
		    selectOnFocus :true,
		    allowBlank :false,
		    lazyRender : true,
		    emptyText:'请选择'
		});
		function rendererCom(value, p, r) {  
			var index = comStore.find(Ext.getCmp('editorBox').valueField, value); 
		    var record = comStore.getAt(index);
		    if (record == null) {  
		        return value;  
		    } else {
		        return record.data.jobTypeName;    
		    }  
		}
		//下拉框--end
		var columnModel=new Ext.grid.ColumnModel([
       		new Ext.grid.RowNumberer(),
			sm,
			{header:"ID",dataIndex:"id",hidden:true},
       		{header:"作业类型",dataIndex:"jobTypeId",width:140,sortable: true,editor:editorBox,renderer: rendererCom},
       		{header:"参数名称",dataIndex:"paraName",width:140,sortable: true,editor:editorC},
       		{header:"参数长度",dataIndex:"paraLen",width:100,sortable: true,editor:editorNum},
       		{header:"参数描述",dataIndex:"paraDesc",width:140,sortable: true,editor:editorC},
       		{header:"参数顺序",dataIndex:"paraSeq",width:100,sortable: true,editor:editorNum}
       	]);
       	
        var record=new Ext.data.Record.create([
			{name:'jobTypeId',type:'string'},
        	{name:'paraName',type:'string'},
        	{name:'paraLen',type:'string'},
        	{name:'paraDesc',type:'string'},
        	{name:'paraSeq',type:'string'}
        ]);
		//工具条
       	var ttbar=new Ext.Toolbar({
			width:600
         });
      	//数据来源
		var store=new Ext.data.Store({
			proxy:new Ext.data.HttpProxy({
			    url:pathUrl+"/listJobTypeParas.do"
			}),
			reader:new Ext.data.JsonReader(
				{
					totalProperty:"count",
					successProperty:"ready",
					root:"list"},
				[{name:"jobTypeId",mapping:"jobTypeId"},
				 {name:"id",mapping:"id"},
				 {name:"paraName",mapping:"paraName"},
				 {name:"paraLen",mapping:"paraLen"},
				 {name:"paraDesc",mapping:"paraDesc"},
				 {name:"paraSeq",mapping:"paraSeq"}
				])
		});

		
		var gridTable=new Ext.grid.EditorGridPanel({
		  	title:'作业类型参数列表',
		  	height : 400,
			width : 800,
			bodyStyle:'padding:5px',
			loadMask : true,
			pruneModifiedRecords:true,
			store:store,
			cm:columnModel,
			sm:sm,
			tbar:[{
   					text:"增加",
   					handler:addRow
	   			},
	    		{
	   				text:"删除",
	   				handler:deletess
	   			},{
	   				text:"保存",
		   			handler:save
	   			}
   			],
   			bbar:new Ext.PagingToolbar({    
   	            pageSize: 10,    
   	            store: store,    
   	            displayInfo: true,    
   	            displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',    
   	            emptyMsg: "没有记录"   
   	        })
			
		});
		store.load({params:{start:0,limit:10}}); 
		function addRow(){
			var insertV={jobTypeId:'',paraName:'',paraLen:'',paraDesc:'',paraSeq:''};
			var re=new record(insertV);
			gridTable.stopEditing();
			store.insert(0,re);
			gridTable.startEditing(0,0);
			re.dirty=true;
			re.modified=insertV;
			if(store.modified.indexOf(re)==-1){
				store.modified.push(re);
			}
		}
		function save(){
			var m=store.getModifiedRecords();
			for(var row = 0;row<m.length;row++){
                var record = m[row].data;
                var jobTypeId = record["jobTypeId"];
                if(jobTypeId==null||jobTypeId==""){
                  Ext.Msg.alert('验证','[作业类型]为必填项');
                  return ;
                }
                var paraName = record["paraName"];
                if(paraName==null||paraName==""){
                    Ext.Msg.alert('验证','[参数名称]为必填项');
                    return ;
                }
                var paraLen = record["paraLen"];
                if(paraLen==null||paraLen==""){
                    Ext.Msg.alert('验证','[参数长度]为必填项');
                    return ;
                }
                var paraDesc = record["paraDesc"];
                if(paraDesc==null||paraDesc==""){
                    Ext.Msg.alert('验证','[参数描述]为必填项');
                    return ;
                }
                var paraSeq = record["paraSeq"];
                if(paraSeq==null||paraSeq==""){
                    Ext.Msg.alert('验证','[参数顺序]为必填项');
                    return ;
                }
            }
			store.commitChanges();
			var jsonArray=[];
			Ext.each(m,function(item){
				jsonArray.push(item.data);
			});
			Ext.Ajax.request({
				method:'post',
				params:{ datas: Ext.util.JSON.encode(jsonArray) },
				url:pathUrl+'/saveJobTypeParas.do',
				callback : function(options, success, response) {
					var json = Ext.util.JSON.decode(response.responseText);
					if (success) {
						if(json.success){
							Ext.MessageBox.alert('提示信息', "保存成功");
							store.reload();
						}else{
							Ext.MessageBox.alert('提示信息', json.message);
						}
					}else{
						 Ext.MessageBox.show({
		                    title: '失败',
		                    msg: '保存失败，请重试！',
		                    buttons: Ext.MessageBox.OK,
		                    icon: Ext.MessageBox.ERROR
		                });
					}
				},
	            waitMsg: '正在处理数据，请稍后……',
	            scope: this
			});
		}
		function deletess(){
			var selections=gridTable.getSelectionModel().getSelections();
			if(selections.length==0){
				 Ext.MessageBox.alert('提示信息','请选择要删除的记录.');
				 return;
			} 
			var ids="";
			for(var i=0;i<selections.length;i++){
				var row=selections[i];
				ids=ids+row.get("id")+"@";
			}
			Ext.Ajax.request({
				method:'post',
				params:{ ids:ids},
				url:pathUrl+'/deleteJobTypeParas.do',
				callback : function(options, success, response) {
					var json = Ext.util.JSON.decode(response.responseText);
					if (success) {
						if(json.success){
							Ext.MessageBox.alert('提示信息', "删除成功");
							store.reload();
						}else{
							Ext.MessageBox.alert('提示信息', json.message);
						}
					}
				},
	            waitMsg: '正在处理数据，请稍后……',
	            scope: this
			});
		}
		gridTable.render("main");
	})
</script>
</body>

</html>

